package demo4;

import demo1.MyTimer;

import java.util.concurrent.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-08-03
 * Time: 15:01
 */
//创建一个自己的线程池--简单地
public class MyThreadPool {

    //将任务放到队列中
    private BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    //用来存放将要执行的任务
    private void submit(Runnable  runnable){
        try {
            queue.put(runnable);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public MyThreadPool(int nThreads){
        for(int i =0;i<nThreads;++i){
            Thread t = new Thread(()->{
                while(!Thread.currentThread().isInterrupted()){
                    try {
                        Runnable runnable = queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }
    }

    public static void main(String[] args) {
        ExecutorService e = Executors.newFixedThreadPool(10);
        for(int i =0;i<10;++i){
            e.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello,world");
                }
            });
        }
    }


}
